Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Nov 13, 2025

This PR implements automatic capture of editor selection as hidden context for LLM messages, allowing users to select text and ask questions like "fix this" or "what does this do" without explicitly pasting code.

Changes

Type Definitions

  • Added selection context fields to WebviewMessage and ExtensionMessage types
  • Fields include: selectedText, selectionFilePath, selectionStartLine, selectionEndLine

Frontend (ChatView)

  • Implemented selection capture on component mount via requestSelectionContext message
  • Selection context is automatically included in all outgoing messages
  • State management for current selection context

Backend (Message Handler)

  • Added requestSelectionContext case to capture active editor selection
  • Converts VSCode 0-based line numbers to 1-based for user-friendly display
  • Handles both workspace-relative and absolute file paths
  • Stores selection context in Task instance

Task Management

  • Added selectionContext property to Task class
  • Selection context is stored before message handling in newTask and askResponse cases
  • Proper type guards ensure all required fields are present before assignment

Environment Details

  • Selection context is included at the start of environment details sent to LLM
  • Formatted as code block with file path and line numbers
  • Automatically clears after use to prevent persistence across messages

Testing

  • Added comprehensive tests to existing test suite
  • Tests cover selection capture, clearing, and inclusion in environment details
  • All tests pass successfully

User Experience

Users can now:

  1. Select text in any editor
  2. Type a message like "fix this" or "explain this"
  3. The selection is automatically included as hidden context with file path and line numbers
  4. The LLM receives the complete context without cluttering the chat UI

The feature seamlessly integrates with the existing message flow and follows established patterns in the codebase.


Important

This PR adds automatic capture of editor selection as context for LLM messages, enhancing user interactions by including selected text and file details without manual input.

  • Behavior:
    • Automatically captures editor selection as context for LLM messages, allowing users to ask questions like "fix this" without pasting code.
    • Selection context includes selectedText, selectionFilePath, selectionStartLine, selectionEndLine.
  • Frontend (ChatView):
    • Captures selection on mount via requestSelectionContext message.
    • Includes selection context in all outgoing messages.
    • Manages state for current selection context.
  • Backend (Message Handler):
    • Handles requestSelectionContext to capture active editor selection.
    • Converts 0-based line numbers to 1-based for display.
    • Supports workspace-relative and absolute file paths.
    • Stores selection context in Task instance.
  • Task Management:
    • Adds selectionContext property to Task class.
    • Stores selection context before message handling in newTask and askResponse cases.
    • Ensures all required fields are present before assignment.
  • Environment Details:
    • Includes selection context at the start of environment details sent to LLM.
    • Formats context as code block with file path and line numbers.
    • Clears context after use to prevent persistence across messages.
  • Testing:
    • Adds tests for selection capture, clearing, and inclusion in environment details.
    • All tests pass successfully.

This description was created by Ellipsis for e25c2d6. You can customize this summary. It will automatically update as commits are pushed.

- Add selection context fields to WebviewMessage and ExtensionMessage types
- Implement selection capture in ChatView on component mount
- Add backend message handler to capture editor selection with line numbers
- Store selection context in Task instance for use in environment details
- Include selection context at start of environment details sent to LLM
- Automatically clear selection context after use to prevent persistence
- Add comprehensive tests for selection context functionality
- Convert VSCode 0-based line numbers to 1-based for user-friendly display
- Handle both workspace-relative and absolute file paths

This allows users to select text in the editor and ask questions like
"fix this" or "what does this do" without explicitly pasting code.
The selection is automatically included as hidden context for the LLM.
@roomote roomote bot requested review from cte, jr and mrubens as code owners November 13, 2025 02:27
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Nov 13, 2025
@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Rooviewer Clock   See task on Roo Cloud

All previous issues resolved. No new issues found.

  • Stale selection context can be sent with messages due to mount-time capture instead of send-time capture
  • Selection context persists in task instance and can become stale between selection changes
  • Selection context not explicitly cleared when tasks are cleared/aborted, risking context leakage
  • Race condition between requesting fresh selection context and sending message with stale context
Previous reviews

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Comment on lines 839 to 843
useMount(() => {
textAreaRef.current?.focus()
// Request initial selection context when component mounts
vscode.postMessage({ type: "requestSelectionContext" })
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale selection context can be sent with messages if the user changes their selection after component mount but before sending a message. The requestSelectionContext call on mount stores the selection in the task, but if the user then selects different text and sends a message, the old selection context will be included. Selection context should be captured at message send time, not at component mount time, to ensure it reflects the user's current selection.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 13, 2025
@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Fixaroo Clock   See task on Roo Cloud

Fixed the reported issues. All local checks passed.

View commit | Revert commit

- Move selection context request from mount-time to send-time in ChatView
- Remove storage of selection context in Task instance to prevent staleness
- Pass selection context through message flow as ephemeral parameter
- Add getAndClearSelectionContext() method to Task for one-time use
- Update tests to reflect new architecture with mocked getter method
- Add selectionContext to CreateTaskOptions interface

This ensures selection context is always fresh when sent and automatically
cleared after use, preventing context leakage between messages.
@mrubens
Copy link
Collaborator

mrubens commented Nov 13, 2025

@roomote why does the webview need to get the selection context? Can't the extension just get it when it needs it?

@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Fixaroo Clock   See task on Roo Cloud

Refactored selection capture to be handled directly by the extension instead of the webview. All local checks passed.

View commit | Revert commit

- Remove selection context state management from ChatView
- Remove requestSelectionContext message type and handler
- Extension now captures selection directly in newTask and askResponse handlers
- Eliminates race condition between selection request and message send
- Simplifies architecture by removing unnecessary webview complexity
@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Fixaroo Clock   Follow along on Roo Cloud

Working on extracting the repeated selection context type definition into a shared type.

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Nov 13, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PR - Needs Preliminary Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: PR [Needs Prelim Review]

Development

Successfully merging this pull request may close these issues.

4 participants